//
//  MyFavoriteViewController.m
//  ManStoreDemo
//
//  Created by qianfeng01 on 15/7/21.
//  Copyright (c) 2015年 xiangmu. All rights reserved.
//

#import "MyFavoriteViewController.h"
#import "FavoriteCell.h"
#include "DetaiModel.h"
#import "DBManager.h"
#import "Detail2ViewController.h"
#import "DetailViewController.h"
#define kScreenSize [UIScreen mainScreen].bounds.size
@interface MyFavoriteViewController ()<UICollectionViewDelegate,UICollectionViewDataSource>
{
    UICollectionView *_collectionView;
    UICollectionViewFlowLayout *_flowLayout;
}
@property (nonatomic,strong) UICollectionView *collectionView;
@property (nonatomic,strong) UICollectionViewFlowLayout *flowLayout;
@property (nonatomic,strong) NSMutableArray *dataArray;
@property (nonatomic,strong) NSArray *arr;

@property (nonatomic,strong) UIButton *deletebutton;

@end

@implementation MyFavoriteViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.dataArray=[[NSMutableArray alloc] init];
    //[self getButton];
    [self showUI];
    if (self.arr.count<1) {
        self.title=@"您还没有收藏";
    }else
    {
        [self setCollectionView];
        
    }
}
-(void)showUI{
    self.arr=[[DBManager sharedManager] readModel];
    for (DetaiModel *model in self.arr) {
        [self.dataArray addObject:model];
       }
    [_collectionView reloadData];
}
//-(void)getButton{
//    UIButton *button=[UIButton buttonWithType:UIButtonTypeSystem];
//    button.frame=CGRectMake(0, 0, 40, 40);
//    [button setTitle:@"编辑" forState:UIControlStateNormal];
//    [button setTitle:@"完成" forState:UIControlStateSelected];
//    [button setTitleColor:[UIColor purpleColor] forState:UIControlStateNormal];
//    [button setTitleColor:[UIColor blueColor] forState:UIControlStateSelected];
//    button.tintColor = [UIColor clearColor];
//    [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
//    UIBarButtonItem *item=[[UIBarButtonItem alloc] initWithCustomView:button];
//    self.navigationItem.rightBarButtonItem=item;
//    
//    self.deletebutton=[UIButton buttonWithType:UIButtonTypeSystem];
//    if (_collectionView.isDecelerating) {
//        self.deletebutton.hidden=YES;
//    }
//}
//-(void)buttonClick:(UIButton *)button{
//    button.selected = !button.selected;
//    if (button.selected==YES) {
//        self.deletebutton.hidden=NO;
//    }else{
//        self.deletebutton.hidden=YES;
//    }
//   
//}
-(void)setCollectionView{
    _flowLayout=[[UICollectionViewFlowLayout alloc] init];
    [_flowLayout setScrollDirection:UICollectionViewScrollDirectionVertical];
    _flowLayout.minimumInteritemSpacing=0;
    _flowLayout.minimumLineSpacing=10;
    self.automaticallyAdjustsScrollViewInsets=NO;
    _collectionView=[[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, kScreenSize.width, kScreenSize.height-64-49) collectionViewLayout:_flowLayout];
    _collectionView.delegate=self;
    _collectionView.dataSource=self;
    [_collectionView registerNib:[UINib nibWithNibName:@"FavoriteCell" bundle:nil] forCellWithReuseIdentifier:@"FavoriteCell"];
    
    _collectionView.backgroundColor=[UIColor grayColor];
    [self.view addSubview:_collectionView];
    
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.dataArray.count;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    FavoriteCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"FavoriteCell" forIndexPath:indexPath];
    
    cell.layer.borderColor=[UIColor blackColor].CGColor;
    cell.layer.borderWidth=2;
    [cell showDataWithIndexPath:indexPath Array:self.dataArray Myblock:^(NSMutableArray *arr) {
        self.dataArray=arr;
        [_collectionView reloadData];
    }];

        return cell;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    return CGSizeMake((kScreenSize.width-30)/2, 230);
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    return UIEdgeInsetsMake(0, 10, 10, 10);
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    DetaiModel *model=self.dataArray[indexPath.row];
    NSLog(@"%@",model.item_id);
    if (model.size==nil) {
        Detail2ViewController *VC=[[Detail2ViewController alloc] init];
        VC.item_id=model.item_id;
        //NSLog(@"%@",VC.item_id);
        [self.navigationController pushViewController:VC animated:YES];
    }else{
        DetailViewController *vc=[[DetailViewController alloc] init];
        vc.item_id=model.item_id;
        [self.navigationController pushViewController:vc animated:YES];
    }
}

@end
